package com.learn.finished;
/*
国际摩尔斯密码定义一种标准编码方式，将每个字母对应于一个由一系列点和短线组成的字符串， 比如:

'a' 对应 ".-" ，
'b' 对应 "-..." ，
'c' 对应 "-.-." ，以此类推。
为了方便，所有 26 个英文字母的摩尔斯密码表如下：

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

给你一个字符串数组 words ，每个单词可以写成每个字母对应摩尔斯密码的组合。

例如，"cab" 可以写成 "-.-..--..." ，(即 "-.-." + ".-" + "-..." 字符串的结合)。我们将这样一个连接过程称作 单词翻译 。
对 words 中所有单词进行单词翻译，返回不同 单词翻译 的数量。
 */

import java.util.HashMap;
import java.util.HashSet;

public class num_804 {
    final static public String[] MORSE={
            ".-","-...","-.-.","-..",".",
            "..-.","--.","....","..",".---",
            "-.-",".-..","--","-.","---",
            ".--.","--.-",".-.","...","-",
            "..-","...-",".--","-..-","-.--","--.."};

    public static int uniqueMorseRepresentations(String[] words) {
        HashSet<String> set = new HashSet<>();
        for (String word : words) {
            StringBuilder code = new StringBuilder();
            for (int i = 0; i < word.length(); i++) {
                char c=word.charAt(i);
                //此处c-'a'是为了找出字符c在26位字母表中的位置
                code.append(MORSE[c-'a']);
            }
            set.add(code.toString());
        }
        return set.size();
    }
    //用哈希表解决，但是感觉效率低了？
    public static int uniqueMorseRepresentations1(String[] words) {
        HashMap<String, Integer> map = new HashMap<>();
        for (String word : words) {
            StringBuilder code = new StringBuilder();
            for (int i = 0; i < word.length(); i++) {
                char c=word.charAt(i);
                code.append(MORSE[c-'a']);
            }
            String res = code.toString();
            map.put(res,map.getOrDefault(res,0)+1);
        }
        int count=0;
        for (Integer value : map.values()) {
            if (value != 1) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        String [] arr=new String[]{"gin","zen","gig","msg"};
        System.out.println(uniqueMorseRepresentations1(arr));
        System.out.println(uniqueMorseRepresentations(arr));
    }

}
